<template>
  <div class="visitor_statistics">
    <div class="selectSite">
      <getSite @handleChange="handleChange" ref="getSite_ref"></getSite>
    </div>
    <div class="visitDetail">
      <div class="visitDetail_header">

        <div class="timeSelect item">
          <!-- 所属时间模块部分 -->
          <commonDatePick :formData='formData' @select-type="selectType" @selectTimeChange="selectTimeChange" ref="commonDatePick_ref"></commonDatePick>
        </div>

        <div class="option-content">
          <!-- <a-button @click="resetOptions">重置</a-button> -->
          <!-- <a-button :style="{ 'margin-right': '8px' }" class="addButton" type="primary"
            @click="getSiteStatistics()">搜索</a-button> -->
        </div>
      </div>
      <div class="tendency_statistics_charts">
        <div class="scrollSection">
          <div class="charts">
            <div class="pv_uvlists">
              <div class="tendency_statistics_item">
                <div class="selectTypes">
                  <span>内容发布总数：</span>
                  {{ dataSource_remote.contentPublishNum }}
                </div>
              </div>
            </div>
            <div class="tendencyOnlinePie_line">
              <div id="chart_site_bar"></div>
            </div>
          </div>
          <!-- 表格展示部分 -->
          <baseTable :columns="columns.contenRelease.site" :dataSource="configData.records" :isSelect="true"
            :scroll="scroll" ref="sensitiveBaseTables" :isShowPagination="isShowPagination" :rowSelection="1"
            :total="configData.total" :indentSize="60" @change="changetable" @changePage="changePage">
            <template #bodyCell="{ column, text, record }">
              <template v-if="column.ellipsis">
                <table-tool-tip :text="text" :column="column"></table-tool-tip>
              </template>
              <template v-if="column.dataIndex == 'sourceUrl'">
                <a class="sourceUrl">{{ text }}</a>
              </template>
            </template>
          </baseTable>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, nextTick, onMounted, onBeforeMount, onUnmounted, reactive, watch, computed } from "vue";
import request from "@/api/index";
import getters from "@/store/getters";
import { initEcharts } from "@/components/echarts/index";
import { optionBar } from "./echartType/options";
import columns from "@/components/table/tableConfig/tableColumn/siteStatistics";
import baseTable from "@/components/table/baseTable.vue";
import tableToolTip from "@/components/table/tableToolTip.vue";
import commonDatePick from '../commonHeader/date.vue';
import getSite from '../commonHeader/getSite.vue';
// type flowColumns
interface dataSourceType {
  [propName: string]: any;
}
//getters
const userInfo = getters.userInfo();
//data
const sectionValue = ref(userInfo.siteId);
const dataSource = ref<dataSourceType>(JSON.parse(JSON.stringify(optionBar)));
dataSource.value.legend.data = ['内容发布统计'];

const showOptionsSelected = ref(1);

const getSite_ref = ref();
//图表展示

const commonDatePick_ref = ref();
let selectTimeRange = ref<(string | number)[]>([]); //选择时间范围
const dataSource_remote = ref<dataSourceType>({});
//表格数据
let configData = reactive<dataSourceType>({ total: 0, size: 10, records: [] });
let scroll: dataSourceType = reactive({ y: 200, isFixed: false }); // 表单滚动设置
const isShowPagination = true; // 是否需要配分页器


const formData = reactive<dataSourceType>({
  selectTimeOption: 1,
  TimeOptions: [{
    label: '今天',
    value: 1
  }, {
    label: '昨天',
    value: 2
  }, {
    label: '最近7天',
    value: 3
  }, {
    label: '最近30天',
    value: 4
  }],
})
let selectAreaData = ref(null); // 选择区域

//分页
const pageNumber = ref(1);
const pageSize = ref(10);
//methods
const changetable = (pagination: any, filters: any, sorter: any,) => {
  let orders = {
    'field': sorter.field,
    'direction': sorter.order.includes('asc') ? "ASC" : 'DESC',
  }
  getSiteStatistics(orders);
}
const handleChange = (value: any) => {//其他选择变化
  nextTick(() => {
    getSiteStatistics();
  });
};
// 获取在线统计数据
const getSiteStatistics = async (orders: any = '') => {
  sectionValue.value = getSite_ref.value.sectionValue;
  selectTimeRange.value = commonDatePick_ref.value.selectTimeRange;
  nextTick(async () => {
    let data: dataSourceType = {
      siteId: sectionValue.value,
      startTime: selectTimeRange.value[0],
      endTime: selectTimeRange.value[1],
      province: selectAreaData.value,
      sourceUrlType: formData.fromSectionValue,
      pageNumber: pageNumber.value,
      pageSize: pageSize.value,
    }
    if (orders) {
      data = Object.assign(data, orders);
    }
    await request.getSiteStatistics(data).then((res: dataSourceType) => {
      dataSource_remote.value = res;
      configData.total = res.pageResult.total;
      configData.records = res.pageResult.records;
    });
    nextTick(() => {
      setData()
    })
  })

}


//选择搜索项
const selectType = (item: dataSourceType, type: string) => {
  formData[type] = item.value;
  pageNumber.value = 1;
  pageSize.value = 10
  nextTick(() => {
    getSiteStatistics();
  })
}
const selectTimeChange = () => {
  selectTimeRange.value = commonDatePick_ref.value.selectTimeRange;
  getSiteStatistics();
}
const setData = () => {
  let res = dataSource_remote.value;
  let name = showOptionsSelected.value == 1 ? '浏览量(PV)' : showOptionsSelected.value == 2 ? '访问数(UV)' : 'IP数';
  dataSource.value.xAxis[0].data = res.nameNumDtoList.map((item: dataSourceType) => item.name);
  dataSource.value.series = [{
    name: '内容发布数',
    type: 'bar',
    barWidth: '40',
    data: res.nameNumDtoList.map((item: dataSourceType) => item.num),
  }]
  initEcharts('chart_site_bar', dataSource.value);
}

//切换分页器
const changePage = (data: { pageCurrent: any; pageSize: any }) => {
  let datas: dataSourceType = {
    pageNumber: data.pageCurrent,
    pageSize: data.pageSize,
  };
  pageNumber.value = data.pageCurrent;
  pageSize.value = data.pageSize;
  getSiteStatistics();
};
//onMounted

onMounted(() => {
  getSiteStatistics();

});

</script>

<style lang="less" scoped>
.visitor_statistics {
  width: 100%;
  height: calc(100% - 72px);
  padding: 16px;
  background: #fff;
  overflow: auto;

  .tips {
    margin-bottom: 24px;
    font-size: 14px;
    color: #333;
  }

  .visitDetail {
    .visitDetail_header {
      display: flex;
      flex-wrap: wrap;
      position: relative;
      width: 90%;
      padding-top: 24px;

      .from {
        font-size: 14px;
        color: #333;
      }

      .timeSelect {
        margin-right: 24px;
      }

      .item {
        height: 40px;
        margin-right: 24px;
        margin-bottom: 24px;
      }

      .ant-select {
        width: 100px;
      }

      .option-content {
        position: absolute;
        right: -10%;
        display: flex;
        top: 18px;
        align-items: center;
        font-size: 14px;
      }
    }

    .charts {
      width: 100%;
      height: 350px;
      border: 1px solid #e8e8e8;
      border-radius: 4px;
      padding: 24px;
      margin-bottom: 24px;

      #chart_site_bar {
        width: 100%;
        height: 350px;
      }
    }
  }
}
</style>
<style lang="scss">
.visitDetail {
  .options {
    display: flex;
    align-items: center;
    font-size: 14px;

    .options_label {
      margin-right: 12px;
    }

    .options_content {
      margin-right: 12px;
      border: 1px solid var(--theme_button_bg_color, #1F6DED);
      display: flex;
      border-radius: 4px;

      span {
        padding: 4px 14px;
        color: #333;
        border-right: 1px solid var(--theme_button_bg_color, #1F6DED);
      }

      span:nth-last-of-type(1) {
        border-right: none;
      }

      span:hover {
        background: var(--theme_button_hover_bg_color, #4892fa);
        border-color: var(--theme_button_hover_bg_color, #4892fa);
        color: #fff;
      }

      .activeOption {
        color: #fff;
        background: var(--theme_button_bg_color, #1F6DED);
      }
    }
  }
}
</style>
<style lang="scss">
.visitor_statistics {
  .ant-table-thead .ant-table-cell:nth-last-of-type(1) {
    text-align: left;
    margin-right: 16px;
  }

  .ant-table-cell {
    .ant-table-row-expand-icon {
      border: none;
      background: transparent;
    }
  }

  .ant-table-expanded-row {
    .ant-table-cell:nth-of-type(1) {
      padding: 0
    }
  }

  .innerTable {
    .ant-table-cell {
      padding-left: 64px !important;
    }

    .ant-table-cell:nth-child(3) {
      padding-left: 52px !important;
    }

    .ant-table-cell:nth-child(4) {
      padding-left: 42px !important;
    }

    .ant-table-cell:nth-child(5) {
      padding-left: 30px !important;
    }


  }

  .ant-table-column-sorters {
    max-width: 100px;
  }
}
</style>